package cn.com.nes.site.service.workOrder.impl;

import cn.com.nes.common.util.PasswordUtils;
import cn.com.nes.common.util.id.AgentIdEnum;
import cn.com.nes.common.util.id.AgentIdUtils;
import cn.com.nes.mybatis.agent.ope.entity.IsRepairStationInfo;
import cn.com.nes.mybatis.agent.ope.mapper.RepairStationMapper;
import cn.com.nes.mybatis.agent.system.entity.TRole;
import cn.com.nes.mybatis.agent.system.entity.TUser;
import cn.com.nes.mybatis.agent.system.mapper.TRoleMapper;
import cn.com.nes.mybatis.agent.system.mapper.TUserMapper;
import cn.com.nes.site.entity.PageData;
import cn.com.nes.site.entity.em.DeleteEnum;
import cn.com.nes.site.entity.vo.workOrder.RepairStationVo;
import cn.com.nes.site.service.system.dict.TParamService;
import cn.com.nes.site.service.system.region.XzqhService;
import cn.com.nes.site.service.system.role.RoleInfoService;
import cn.com.nes.site.service.workOrder.RepairStationService;
import cn.com.nes.site.service.workOrder.dto.RepairStationDto;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: ShenWei
 * @Date: 2024/03/06/14:30
 */
@Deprecated
@Service
public class RepairStationServiceImpl implements RepairStationService {
    @Autowired
    XzqhService xzqhService;

    @Autowired
    RoleInfoService roleInfoService;

    @Autowired
    TParamService tParamService;

    @Resource
    RepairStationMapper repairStationMapper;

    @Resource
    TUserMapper tUserMapper;

    @Resource
    TRoleMapper tRoleMapper;

    @Override
    public PageData getRepairStations(RepairStationVo vo) {
        String areaCode = xzqhService.getAreaCodePrefix(vo.getAreaCode());
        vo.setAreaCode(areaCode);

        Page<RepairStationDto> page = new Page<>(vo.getPageid(), vo.getPagesize());
        List<RepairStationDto> list = repairStationMapper.getStationList(page,vo);
        return PageData.ok(page,list);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public PageData delRepairStation(RepairStationVo vo) {
        // 删除站点信息 - 是个物理删除,等后期再优化吧
        repairStationMapper.delRepairStation(vo.getStationId());
        // 删除站点负责人信息
        tUserMapper.delUserByUuid(vo.getStationId());
        return PageData.ok();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String saveRepairStation(RepairStationVo vo) throws Exception {
        //查询手机号, 不允许存在相同的手机号
        Integer checkPhoneNumberDuplicated = tUserMapper.checkPhoneNumberDuplicated(vo.getPhone(), vo.getStationId());
        //判断该手机号是否被其他用户使用
        if (checkPhoneNumberDuplicated > 0) {
            return "-1";
        }
        String stationId = vo.getStationId();
        // 设置站点信息
        IsRepairStationInfo stationInfo = new IsRepairStationInfo();
        stationInfo.setAddress(vo.getAddress());    // 详细地址
        stationInfo.setName(vo.getStationName());   // 站点名称
        stationInfo.setPhone(vo.getPhone());        // 联系电话
        stationInfo.setPersonInCharge(vo.getPersonInCharge());  // 负责人姓名
        stationInfo.setManufactureId(vo.getManufactureId());    // 厂商id
        stationInfo.setDepartmentId(vo.getDepartmentId());      // 部门id
        if (NumberUtil.isNumber(vo.getLongitude())) {
            stationInfo.setJd(new BigDecimal(vo.getLongitude()));     // 经度
        }
        if (NumberUtil.isNumber(vo.getLatitude())) {
            stationInfo.setWd(new BigDecimal(vo.getLatitude()));      // 维度
        }
        String areaCode = vo.getAreaCode();
        if (StrUtil.isNotBlank(areaCode)) {
            areaCode = areaCode.trim().substring(0, 4) + "00000000";
        }
        stationInfo.setXzqhdm(areaCode);                        // 行政区划代码

        // 设置角色信息 - 在用户表维护一类角色，关联站点的关系，不是真的用户
        TUser userInfo = new TUser();
        userInfo.setUsername(vo.getPhone()); // 用户名是手机号
        userInfo.setPassword(PasswordUtils.encrypt(vo.getPhone()));
        userInfo.setNickName(vo.getStationName());
        userInfo.setDepartmentId(vo.getDepartmentId());
        userInfo.setPhone(vo.getPhone());
        userInfo.setAreacode(vo.getAreaCode());
        userInfo.setLongitude(new BigDecimal(vo.getLongitude())); // 经度
        userInfo.setLatitude(new BigDecimal(vo.getLatitude())); // 维度
        userInfo.setModifyUid(Long.valueOf(vo.getUserInfo().getId()));
        userInfo.setModifyTime(new Date());

        //没有维修站id则新增一个维修站
        if (StrUtil.isBlank(stationId)) {
            // 插入站点信息
            stationId = AgentIdUtils.createID(AgentIdEnum._repair_station);
            stationInfo.setStationId(stationId);
            repairStationMapper.insert(stationInfo);
            // 插入维修站用户信息
            userInfo.setUuid(stationId);
            userInfo.setStationId(stationId);
            userInfo.setDataRange(2); // 迁移代码也不知道为啥是2
            userInfo.setSex("");
            String roleId = getRepairStationRole(vo);
            userInfo.setRoleId(roleId);   // 角色id要和厂商id绑定
            userInfo.setAreaname("");
            userInfo.setCompanyId(vo.getUserInfo().getCompanyId());
            userInfo.setIdentity("");
            userInfo.setUserType("");   // 职务待定
            userInfo.setCreateUid(Long.valueOf(vo.getUserInfo().getId()));
            userInfo.setCreateTime(new Date());
            userInfo.setIsassociated("");
            userInfo.setRelevanceid("");
            userInfo.setSfsc(0); // 没删除
            userInfo.setIsOnlineReg(0); // 不是在线注册
            tUserMapper.insert(userInfo);
        } else {
            // 更新站点信息
            stationInfo.setStationId(stationId);
            repairStationMapper.updateByStationId(stationInfo);
            // 更新角色信息
            userInfo.setUuid(stationId);
            tUserMapper.updateByUuid(userInfo);
        }
        return stationId;
    }

    // 获取维修站点角色
    String getRepairStationRole(RepairStationVo vo) {
        String manufactureId = vo.getManufactureId();
        String companyId = vo.getUserInfo().getCompanyId();
        Long userId = Long.valueOf(vo.getUserInfo().getId());
        //查询是否有维修网点角色
        String roleId = tRoleMapper.getRepairStationRole(manufactureId);
        if (StrUtil.isBlank(roleId)) {
            String appAuthority = "model_18486369457587051759228522024126566,model_18486144443070655759228521528152231";    // app权限
            String pcAuthority = "model_00036,model_30872210675309184759210407205184885,model_32077412478122933759248682121079649";     // pc权限
            roleId = AgentIdUtils.createID(AgentIdEnum._role);
            // 组装参数
            TRole tRole = new TRole();
            tRole.setRoleId(roleId);
            tRole.setName("维修网点");
            tRole.setDesc("维修网点");
            tRole.setEnabled(true);
            tRole.setIsAuthorRole("0");
            tRole.setCompanyId(companyId);
            tRole.setSfsc(DeleteEnum._0.getKey());
            tRole.setCreateUid(userId);
            tRole.setCreateTime(new Date());
            tRole.setModifyUid(userId);
            tRole.setModifyTime(new Date());
            tRole.setUuid(roleId);
            tRole.setManufactureId(manufactureId);

            tRoleMapper.insert(tRole);

            // TODO: 2024/3/12 暂时没考虑角色权限问题
        }
        return roleId;
    }
}
